package com.ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssm.entity.User;
import com.ssm.service.ArticleServiceImpl;
import jakarta.servlet.http.HttpSession;

@Controller
@RequestMapping("article")
public class ArticleController {
    @Autowired
    private ArticleServiceImpl articleService;
    
    @RequestMapping("/categoryShow")
    public String categoryShow(){        
        return "article_category :: content";
    }
    
    @RequestMapping("/tagShow")
    public String tagShow(){        
        return "article_tag :: content";
    }
    
    @RequestMapping("/show")
    public String show(){        
        return "article :: content";
    }
    
    @RequestMapping("/categoryList")
    @ResponseBody
    public String categoryList(){
        return articleService.findAllCategories();
    }
    
    @RequestMapping("/tagList")
    @ResponseBody
    public String tagList(){
        return articleService.findAllTags();
    }
    
    @RequestMapping("/list")
    @ResponseBody
    public String list(){
        return articleService.findAll();
    }
    
    @RequestMapping("/listByUser")
    @ResponseBody
    public String listByUser(HttpSession session){
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser != null) {
            return articleService.findByUser(loginUser.getUid());
        }
        return "{\"data\":[]}";
    }
    
    @GetMapping("/category/{cid}")
    @ResponseBody
    public String getCategory(@PathVariable int cid){
        return articleService.getCategory(cid);
    }
    
    @GetMapping("/tag/{tid}")
    @ResponseBody
    public String getTag(@PathVariable int tid){
        return articleService.getTag(tid);
    }
    
    @GetMapping("/get/{aid}")
    @ResponseBody
    public String getArticle(@PathVariable int aid){
        return articleService.get(aid);
    }
    
    @GetMapping("/tags/{aid}")
    @ResponseBody
    public String getArticleTags(@PathVariable int aid){
        return articleService.getArticleTags(aid);
    }
    
    @RequestMapping("/parentCategories")
    @ResponseBody
    public String parentCategories(){
        return articleService.findParentCategories();
    }
    
    @RequestMapping("/addCategory")
    @ResponseBody
    public String addCategory(String cname, String description, int parent_id){
        return articleService.addCategory(cname, description, parent_id);
    }
    
    @RequestMapping("/updateCategory")
    @ResponseBody
    public String updateCategory(int cid, String cname, String description, int parent_id){
        return articleService.updateCategory(cid, cname, description, parent_id);
    }
    
    @RequestMapping("/deleteCategory")
    @ResponseBody
    public String deleteCategory(int cid){
        return articleService.deleteCategory(cid);
    }
    
    @RequestMapping("/addTag")
    @ResponseBody
    public String addTag(String tname, String description){
        return articleService.addTag(tname, description);
    }
    
    @RequestMapping("/updateTag")
    @ResponseBody
    public String updateTag(int tid, String tname, String description){
        return articleService.updateTag(tid, tname, description);
    }
    
    @RequestMapping("/deleteTag")
    @ResponseBody
    public String deleteTag(int tid){
        return articleService.deleteTag(tid);
    }
    
    @RequestMapping("/add")
    @ResponseBody
    public String add(String title, String content, String summary, int cid, HttpSession session){
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser != null) {
            return articleService.add(title, content, summary, cid, loginUser.getUid());
        }
        return "{\"Status\":\"error\",\"msg\":\"用户未登录\"}";
    }
    
    @RequestMapping("/update")
    @ResponseBody
    public String update(int aid, String title, String content, String summary, int cid, HttpSession session){
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser != null) {
            return articleService.update(aid, title, content, summary, cid, loginUser.getUid());
        }
        return "{\"Status\":\"error\",\"msg\":\"用户未登录\"}";
    }
    
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(int aid, HttpSession session){
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser != null) {
            return articleService.delete(aid, loginUser.getUid());
        }
        return "{\"Status\":\"error\",\"msg\":\"用户未登录\"}";
    }
    
    @RequestMapping("/addTagToArticle")
    @ResponseBody
    public String addTagToArticle(int aid, int tid, HttpSession session){
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser != null) {
            return articleService.addTagToArticle(aid, tid);
        }
        return "{\"Status\":\"error\",\"msg\":\"用户未登录\"}";
    }
    
    @RequestMapping("/removeTagFromArticle")
    @ResponseBody
    public String removeTagFromArticle(int aid, int tid, HttpSession session){
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser != null) {
            return articleService.removeTagFromArticle(aid, tid);
        }
        return "{\"Status\":\"error\",\"msg\":\"用户未登录\"}";
    }
} 